package com.defysoft.waukids.slide;

import java.util.Calendar;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.Data_Type.School_Detail;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.PublicSharedpreferences.SF_School_Key;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;

public class Main_Slide_Option extends BaseActivity implements OnClickListener, OnCheckedChangeListener{
	Toolbar toolbar;
	String PK_Mem_Email;

	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	SharedPreferences.Editor editor, editor_school ;
	SharedPreferences notifications;
	EditText editText[] = new EditText[4]; 	//방해금지시간시작,종료. cctv 시작,종료.
	String push[] = new String[8]; // 소리, 진동, 푸시, 공지, 현장학습, 문서요청서, CCTV

	String time_choice_start_stop, cctv_choice_start_stop;
	int startHourCctv, startMinuteCctv, stopHourCctv, stopMinuteCctv, startTimeCctv, stopTimeCctv = 0;
	int startHour, startMinute, stopHour, stopMinute, startTime, stopTime = 0;
	//초기화 버튼
	Button button[] = new Button[4];    //방해금지시간시작,종료. cctv 시작,종료.

	//라디오버튼
	RadioGroup radio[] = new RadioGroup[8];
	RadioButton radioButton_On[] = new RadioButton[8];
	RadioButton radioButton_Off[] = new RadioButton[8];
	
	
	//메세지 문서 옵션
	private final int DOCUMENT_CHANGE = 1;
	private final int SUCCESS_DOCUMENT = 2;
	private final int FAIL_DOCUMENT = 3;
	
	int document_choice=0;
	int document_group_click =0;
	//CCTV 켜고끄기.
	private final int CCTV_CHANGE = 4;
	private final int SUCCESS_CCTV = 5;
	private final int FAIL_CCTV = 6;
	//CCTV 시간입력스레드.
	private final int CCTV_TIME_CHANGE = 7;
	private final int SUCCESS_CCTV_TIME = 8;
	private final int FAIL_CCTV_TIME = 9;
	//CCTV 시간 DB LOAD스레드
	private final int CCTV_TIME_LOAD = 10;
	private final int SUCCESS_CCTV_TIME_LOAD = 11;
	private final int FAIL_CCTV_TIME_LOAD = 12;

	int Sch_Cctv_Time_Start, Sch_Cctv_Time_Stop;
	int cctv_choice=0;
	int cctv_group_click =0;	
	//시간겹처서 빨간색인지.
	int cctv_time_red =0;	
	public Data_Sharedpreferences.SF_School school;
	String db_cctv_start_hour, db_cctv_start_minute, db_cctv_stop_hour, db_cctv_stop_minute ="0";
	School_Detail school_detail;
	//cctv시간선택
	TextView textView_cctv;
	
	public Sqlite_Connect sqlite_Connect;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.main_slide_option_activity);
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();
		psf.setSchoolsp();
		school = psf.getSchoolsp();

		
		PK_Mem_Email = member.Email;
		notifications = getSharedPreferences("notifications", MODE_PRIVATE);
		editor = notifications.edit();
		if (PK_Mem_Email == null || PK_Mem_Email == "") {
			Toast.makeText(this, "잘못된 접근입니다.", Toast.LENGTH_SHORT).show();
		}
		setToolbar();
		setStartData();
		
		registerReceiver(mToastMessageReceiver, new IntentFilter(
				"com.defysoft.waukids.TOAST_MESSAGE"));
	}

	@Override
	public void setStartData() {
		super.setStartData();
		sqlite_Connect = new Sqlite_Connect(this);
		editText[0] = (EditText) findViewById(R.id.MAIN_SLIDE_OPTION_EDITTEXT_ALARM_START);
		editText[1] = (EditText) findViewById(R.id.MAIN_SLIDE_OPTION_EDITTEXT_ALARM_STOP);
		radio[0] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_NOTIFICATIONS);
		radio[1] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_ATTENDANCE);
		radio[2] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_NOTICE);
		radio[3] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_FIELD);
		radio[4] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_SOUND);
		radio[5] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_VIBRATION);
		radio[6] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_DOCUMENT);
		radio[7] = (RadioGroup) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOGROUP_CCTV);
		radioButton_On[0] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTIFICATIONS_ON);
		radioButton_On[1] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_ATTENDANCE_ON);
		radioButton_On[2] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTICE_ON);
		radioButton_On[3] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_FIELD_ON);
		radioButton_On[4] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_SOUND_ON);
		radioButton_On[5] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_VIBRATION_ON);
		radioButton_On[6] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_DOCUMENT_ON);
		radioButton_On[7] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_CCTV_ON);
		radioButton_Off[0] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTIFICATIONS_OFF);
		radioButton_Off[1] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_ATTENDANCE_OFF);
		radioButton_Off[2] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTICE_OFF);
		radioButton_Off[3] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_FIELD_OFF);
		radioButton_Off[4] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_SOUND_OFF);
		radioButton_Off[5] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_VIBRATION_OFF);
		radioButton_Off[6] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_DOCUMENT_OFF);
		radioButton_Off[7] = (RadioButton) findViewById(R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_CCTV_OFF);
		button[0] = (Button)findViewById(R.id.MAIN_SLIDE_OPTION_BUTTON_ALARM_START);
		button[1] = (Button)findViewById(R.id.MAIN_SLIDE_OPTION_BUTTON_ALARM_STOP);
		//CCTV
		editText[2] = (EditText) findViewById(R.id.MAIN_SLIDE_OPTION_EDITTEXT_CCTV_START);
		editText[3] = (EditText) findViewById(R.id.MAIN_SLIDE_OPTION_EDITTEXT_CCTV_STOP);		
		button[2] = (Button)findViewById(R.id.MAIN_SLIDE_OPTION_BUTTON_CCTV_START);
		button[3] = (Button)findViewById(R.id.MAIN_SLIDE_OPTION_BUTTON_CCTV_STOP);		
		textView_cctv = (TextView)findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_APPLY_BTN);
		textView_cctv.setOnClickListener(this);
		
		if (member.Rank.equals("1")) {
			findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_RANK_ONE)
					.setVisibility(View.VISIBLE);
			findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_RANK_TWO)
					.setVisibility(View.VISIBLE);
		/*	findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_RANK_THREE)
			.setVisibility(View.VISIBLE);
			findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_RANK_FOUR)
			.setVisibility(View.VISIBLE);*/
		}
	/*	if (member.Rank.equals("1")) {
			startNetwork(CCTV_TIME_LOAD);
		}
		
		//setCctvNow();//cctv설정시간.
*/
		setnow(); // 알림설정 현재값으로.
		//수신시작 세팅.
		editText[0].addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				final String ss = s.toString();
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {			}
			@Override
			public void afterTextChanged(Editable s) {			}
		});
		for (int i = 0; i < editText.length; i++) {
			editText[i].setOnClickListener(this);
		}
		for (int i = 0; i < radio.length; i++) {
			radio[i].setOnCheckedChangeListener(this);
		}
		for (int i = 0; i < button.length; i++) {
			button[i].setOnClickListener(this);
		}
	}

	private void setCctvNow() {
		//cctv시간 자르기.
		if (school.Cctv_Time_Start.length()==3) {
			db_cctv_start_hour = school.Cctv_Time_Start.substring(0, 1);
			db_cctv_start_minute = school.Cctv_Time_Start.substring(1, 3);
		} else if (school.Cctv_Time_Start.length()==4) {
			db_cctv_start_hour = school.Cctv_Time_Start.substring(0, 2);
			db_cctv_start_minute = school.Cctv_Time_Start.substring(2, 4);
		} else if (school.Cctv_Time_Start.length()==2) {
		db_cctv_start_hour = "0";
		db_cctv_start_minute = school.Cctv_Time_Start.substring(0, 2);
	} else if (school.Cctv_Time_Start.length()==1) {
		db_cctv_start_hour = "0";
		db_cctv_start_minute = school.Cctv_Time_Start.substring(0, 1);
	}
		if (school.Cctv_Time_Stop.length()==3) {
			db_cctv_stop_hour = school.Cctv_Time_Stop.substring(0, 1);
			db_cctv_stop_minute = school.Cctv_Time_Stop.substring(1, 3);
		} else if (school.Cctv_Time_Stop.length()==4) {
			db_cctv_stop_hour = school.Cctv_Time_Stop.substring(0, 2);
			db_cctv_stop_minute = school.Cctv_Time_Stop.substring(2, 4);
		} else if (school.Cctv_Time_Stop.length()==2) {
			db_cctv_stop_hour = "0";
			db_cctv_stop_minute = school.Cctv_Time_Stop.substring(0, 2);
		} else if (school.Cctv_Time_Stop.length()==1) {
			db_cctv_stop_hour = "0";
			db_cctv_stop_minute = school.Cctv_Time_Stop.substring(0, 1);
		}	

			startHourCctv = Integer.parseInt(db_cctv_start_hour);
			startMinuteCctv = Integer.parseInt(db_cctv_start_minute);
			editText[2].setText(startHourCctv + " 시 " + startMinuteCctv + " 분 ");			

		startTimeCctv = (startHourCctv*100)+startMinuteCctv;

			stopHourCctv = Integer.parseInt(db_cctv_stop_hour);
			stopMinuteCctv = Integer.parseInt(db_cctv_stop_minute);
			editText[3].setText(stopHourCctv + " 시 " + stopMinuteCctv + " 분 ");			

		stopTimeCctv = (stopHourCctv*100)+stopMinuteCctv;		
		//CCTV사용하면 시간설정켜기. 사용안하면 시간설정 끄기.
 
		if(school.Cctv.equals("0")) {
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_TIMER).setVisibility(View.VISIBLE);
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_START).setVisibility(View.VISIBLE);
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_STOP).setVisibility(View.VISIBLE);
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_APPLY).setVisibility(View.VISIBLE);
	} else {
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_TIMER).setVisibility(View.GONE);
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_START).setVisibility(View.GONE);
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_STOP).setVisibility(View.GONE);
		findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_APPLY).setVisibility(View.GONE);
	}
	}

	private void setnow() { // 현재 알림값으로 초기화.
		//방해금지시간.
		if (notifications.getInt("START_TIMEPICKER_TIME", 0) != 0 ) {
			startHour = notifications.getInt("START_TIMEPICKER_HOUR", 0);
			startMinute = notifications.getInt("START_TIMEPICKER_MINUTE", 0);
			startTime = startHour * 100 + startMinute;
			editText[0].setText(startHour + " 시 " + startMinute + " 분 ");
		} else{
		}
		if (notifications.getInt("STOP_TIMEPICKER_TIME", 0) != 0) {
			stopHour = notifications.getInt("STOP_TIMEPICKER_HOUR", 0);
			stopMinute = notifications.getInt("STOP_TIMEPICKER_MINUTE", 0);
			stopTime  = stopHour * 100 + stopMinute;
			editText[1].setText(stopHour + " 시 " + stopMinute + " 분 ");
		}

		push[0] = notifications.getString("PUSH_NOTIFICATIONS", "0");
		push[1] = notifications.getString("PUSH_ATTENDANCE", "0");
		push[2] = notifications.getString("PUSH_NOTICE", "0");
		push[3] = notifications.getString("PUSH_FIELD", "0");
		push[4] = notifications.getString("PUSH_SOUND", "0");
		push[5] = notifications.getString("PUSH_VIBRATION", "0");
		push[6] = notifications.getString("PUSH_DOCUMENT", "0");
		push[7] = notifications.getString("PUSH_CCTV", "0");
		
		for (int i = 0; i < push.length; i++) {
			if (push[i].equals("0")) {
				radioButton_On[i].setChecked(true);
			} else {
				radioButton_Off[i].setChecked(true);				
			}
		}
		
	}

	private void setToolbar() {
		toolbar = (Toolbar) findViewById(R.id.MAIN_SLIDE_OPTION_TOOLBAR);
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		DialogFragment newFragment = new TimePickerFragment();
		time_choice_start_stop = "";
		cctv_choice_start_stop = "";
		switch (v.getId()) {
		case R.id.MAIN_SLIDE_OPTION_BUTTON_ALARM_START:
			alertDialog("수신시작 초기화 하겠습니까?", "start");
			break;
		case R.id.MAIN_SLIDE_OPTION_BUTTON_ALARM_STOP:
			alertDialog("수신종료 초기화 하겠습니까?", "stop");
			break;
		case R.id.MAIN_SLIDE_OPTION_EDITTEXT_ALARM_START:
			time_choice_start_stop = "1";
			newFragment.show(getSupportFragmentManager(), "timePicker");
			break;
		case R.id.MAIN_SLIDE_OPTION_EDITTEXT_ALARM_STOP:
			time_choice_start_stop = "2";
			newFragment.show(getSupportFragmentManager(), "timePicker");
			break;
			//CCTV 추가
		case R.id.MAIN_SLIDE_OPTION_BUTTON_CCTV_START:
			alertDialogCCTV("수신시작 초기화 하겠습니까?", "start");
			break;
		case R.id.MAIN_SLIDE_OPTION_BUTTON_CCTV_STOP:
			alertDialogCCTV("수신종료 초기화 하겠습니까?", "stop");
			break;
		case R.id.MAIN_SLIDE_OPTION_EDITTEXT_CCTV_START:
			cctv_choice_start_stop = "1";
			newFragment.show(getSupportFragmentManager(), "cctvPicker");
			break;
		case R.id.MAIN_SLIDE_OPTION_EDITTEXT_CCTV_STOP:
			cctv_choice_start_stop = "2";
			newFragment.show(getSupportFragmentManager(), "cctvPicker");
			break;		
		case R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_APPLY_BTN:		//CCTV설정시간 저장.
			//DB입력스레드
			if (cctv_time_red == 1) {
				Toast.makeText(Main_Slide_Option.this,
						"수신시작시간이 수신종료시간보다 전 이어야 합니다1", Toast.LENGTH_SHORT)
						.show();
			} else {
			Sch_Cctv_Time_Start = startTimeCctv;
			Sch_Cctv_Time_Stop = stopTimeCctv;
			alertDialogCCTVtime("시간을 저장 하겠습니까?", CCTV_TIME_CHANGE);
			//startNetwork(CCTV_TIME_CHANGE);
			}
			break;		
	
		}
	}

	public void time(int hourOfDay, int minute) {

	}

	public class TimePickerFragment extends DialogFragment implements
			TimePickerDialog.OnTimeSetListener {

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			final Calendar c = Calendar.getInstance();
			int hour = c.get(Calendar.HOUR_OF_DAY);
			int minute = c.get(Calendar.MINUTE);

			// Create a new instance of TimePickerDialog and return it
			return new TimePickerDialog(getActivity(), this, hour, minute, true);
		}
		
		
		//시간 선택 후
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			
			if (time_choice_start_stop.equals("1")) {	//스타트시간
				startHour = hourOfDay;
				startMinute = minute;
				startTime = startHour * 100 + startMinute;
				if (startHour * 100 + startMinute >= stopHour * 100 + stopMinute
						&& stopHour * 100 + stopMinute != 0) {
					editText[0].setText(startHour + " 시 " + startMinute + " 분");
					Toast.makeText(Main_Slide_Option.this,
							"1수신시작시간이 수신종료시간보다 전 이어야 합니다", Toast.LENGTH_SHORT)
							.show();
					editText[0].setText("");
					editText[1].setText("");
					editText[0].setHint("다시 설정하여 주세요.");
					editText[1].setHint("다시 설정하여 주세요.");
					editText[0].setHintTextColor(Color.RED);
					editText[1].setHintTextColor(Color.RED);
					editor.putInt("START_TIMEPICKER_HOUR", 0);
					editor.putInt("START_TIMEPICKER_MINUTE", 0);
					editor.putInt("START_TIMEPICKER_TIME", 0);
					editor.putInt("STOP_TIMEPICKER_HOUR", 0);
					editor.putInt("STOP_TIMEPICKER_MINUTE", 0);
					editor.putInt("STOP_TIMEPICKER_TIME", 0);
					editor.putInt("START_TIMEPICKER_CHECK", 0); 
					editor.putInt("STOP_TIMEPICKER_CHECK", 0);
					editor.commit();
					startHour = 0;
					startMinute = 0;
					startTime = 0;
					stopHour= 0;
					stopMinute = 0;
					stopTime = 0;
				} else {

					editText[0].setText(startHour + " 시 " + startMinute + " 분");
					editText[0].setTextColor(Color.BLACK);
					
					if (stopTime == 0 || stopTime  < 1) {
						editText[1].setTextColor(Color.RED);						
					}else{
						editText[1].setText(stopHour + " 시 " + stopMinute + " 분 ");
						editText[1].setTextColor(Color.BLACK);								
					}
					editor.putInt("START_TIMEPICKER_HOUR", startHour);
					editor.putInt("START_TIMEPICKER_MINUTE", startMinute);
					editor.putInt("START_TIMEPICKER_TIME", startTime);
					editor.putInt("STOP_TIMEPICKER_HOUR", stopHour);
					editor.putInt("STOP_TIMEPICKER_MINUTE", stopMinute);
					editor.putInt("STOP_TIMEPICKER_TIME", stopTime);
					editor.putInt("START_TIMEPICKER_CHECK", 1); 
					editor.commit();

				}

			} else if (time_choice_start_stop.equals("2")) {	//스탑시간
				stopHour = hourOfDay;
				stopMinute = minute;
				stopTime =stopHour * 100 + stopMinute;
				if (startHour * 100 + startMinute >= stopHour * 100
						+ stopMinute && startHour * 100 + startMinute != 0) {

					editText[1].setText(stopHour + " 시 " + stopMinute + " 분 ");
					Toast.makeText(Main_Slide_Option.this,
							"2수신시작시간이 수신종료시간보다 전 이어야 합니다", Toast.LENGTH_SHORT)
							.show();
					editText[0].setText("");
					editText[1].setText("");
					editText[0].setHint("다시 설정하여 주세요.");
					editText[1].setHint("다시 설정하여 주세요.");
					editText[0].setHintTextColor(Color.RED);
					editText[1].setHintTextColor(Color.RED);
					
					editor.putInt("START_TIMEPICKER_HOUR", 0);
					editor.putInt("START_TIMEPICKER_MINUTE", 0);
					editor.putInt("START_TIMEPICKER_TIME", 0);
					editor.putInt("STOP_TIMEPICKER_HOUR", 0);
					editor.putInt("STOP_TIMEPICKER_MINUTE", 0);
					editor.putInt("STOP_TIMEPICKER_TIME", 0);
					editor.putInt("START_TIMEPICKER_CHECK", 0); 
					editor.putInt("STOP_TIMEPICKER_CHECK", 0);
					editor.commit();
					startHour = 0;
					startMinute = 0;
					startTime = 0;
					stopHour= 0;
					stopMinute = 0;
					stopTime = 0;
				} else {


					editText[1].setText(stopHour + " 시 " + stopMinute + " 분 ");
					editText[1].setTextColor(Color.BLACK);
					if (startTime == 0 || startTime  < 1) {
						editText[0].setTextColor(Color.RED);						
					}else{
						editText[0].setText(startHour + " 시 " + startMinute + " 분 ");
						editText[0].setTextColor(Color.BLACK);								
					}
					editText[0].setTextColor(Color.BLACK);
					editor.putInt("START_TIMEPICKER_HOUR", startHour);
					editor.putInt("START_TIMEPICKER_MINUTE", startMinute);
					editor.putInt("START_TIMEPICKER_TIME", startTime);
					editor.putInt("STOP_TIMEPICKER_HOUR", stopHour);
					editor.putInt("STOP_TIMEPICKER_MINUTE", stopMinute);
					editor.putInt("STOP_TIMEPICKER_TIME", stopTime);
					editor.putInt("STOP_TIMEPICKER_CHECK", 1);
					editor.commit();
				}
			}
//CCTV
			if (cctv_choice_start_stop.equals("1")) {
				startHourCctv = hourOfDay;
				startMinuteCctv = minute;
				startTimeCctv = startHourCctv * 100 + startMinuteCctv;
				if (startHourCctv * 100 + startMinuteCctv >= stopHourCctv * 100 + stopMinuteCctv
						&& stopHourCctv * 100 + stopMinuteCctv != 0) {
					editText[2].setText(startHourCctv + " 시 " + startMinuteCctv + " 분");
					Toast.makeText(Main_Slide_Option.this,
							"시작시간이 종료시간보다 전 이어야 합니다2", Toast.LENGTH_SHORT)
							.show();
					editText[2].setTextColor(Color.RED);
					editText[3].setTextColor(Color.RED);
					cctv_time_red =1;
				} else {
					editText[2].setText(startHourCctv + " 시 " + startMinuteCctv + " 분");
					editText[2].setTextColor(Color.BLACK);
					editText[3].setTextColor(Color.BLACK);
					cctv_time_red=0;
				}
			} else if (cctv_choice_start_stop.equals("2")) {
				stopHourCctv = hourOfDay;
				stopMinuteCctv = minute;
				stopTimeCctv =stopHourCctv * 100 + stopMinuteCctv;
				if (startHourCctv * 100 + startMinuteCctv >= stopHourCctv * 100
						+ stopMinuteCctv && startHourCctv * 100 + startMinuteCctv != 0) {
					editText[3].setText(stopHourCctv + " 시 " + stopMinuteCctv + " 분 ");
					Toast.makeText(Main_Slide_Option.this,
							"시작시간이 종료시간보다 전 이어야 합니다.3", Toast.LENGTH_SHORT)
							.show();
					editText[2].setTextColor(Color.RED);
					editText[3].setTextColor(Color.RED);

					cctv_time_red =1;
				} else {
					editText[3].setText(stopHourCctv + " 시 " + stopMinuteCctv + " 분 ");
					editText[2].setTextColor(Color.BLACK);
					editText[3].setTextColor(Color.BLACK);
					cctv_time_red =0;
				}
			}
		}

	}
	
	private final BroadcastReceiver mToastMessageReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null);
			TextView toast_tv = (TextView)layout.findViewById(R.id.TOAST_LAYOUT_TV);
			Toast toast = new Toast(Main_Slide_Option.this);
			toast.setView(layout);
			toast_tv.setText(intent.getStringExtra("PUSH_NAME")+" : "+intent.getStringExtra("PUSH_SUB"));
			toast.setDuration(Toast.LENGTH_LONG);
			toast.show();
		}
	};
//라디오버튼 바뀌었을때.
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (group.getId()) {
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_NOTIFICATIONS:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTIFICATIONS_ON:
				editor.putString("PUSH_NOTIFICATIONS", "0");
				editor.commit();				
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTIFICATIONS_OFF:
				editor.putString("PUSH_NOTIFICATIONS", "1");
				editor.commit();				
				break;
			}
			break;
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_ATTENDANCE:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_ATTENDANCE_ON:
				editor.putString("PUSH_ATTENDANCE", "0");
				editor.commit();				
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_ATTENDANCE_OFF:
				editor.putString("PUSH_ATTENDANCE", "1");
				editor.commit();				
				break;
			}			
			break;
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_NOTICE:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTICE_ON:
				editor.putString("PUSH_NOTICE", "0");
				editor.commit();				
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_NOTICE_OFF:
				editor.putString("PUSH_NOTICE", "1");
				editor.commit();				
				break;
			}			
			break;
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_FIELD:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_FIELD_ON:
				editor.putString("PUSH_FIELD", "0");
				editor.commit();				
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_FIELD_OFF:
				editor.putString("PUSH_FIELD", "1");
				editor.commit();				
				break;
			}			
			break;
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_SOUND:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_SOUND_ON:
				editor.putString("PUSH_SOUND", "0");
				editor.commit();				
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_SOUND_OFF:
				editor.putString("PUSH_SOUND", "1");
				editor.commit();				
				break;
			}			
			break;
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_VIBRATION:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_VIBRATION_ON:
				editor.putString("PUSH_VIBRATION", "0");
				editor.commit();				
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_VIBRATION_OFF:
				editor.putString("PUSH_VIBRATION", "1");
				editor.commit();				
				break;
			}			
			break;
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_DOCUMENT:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_DOCUMENT_ON:
					if (document_group_click == 0) {
					document_choice = 0;
					alertDialog("메세지(투약요청서,귀가의뢰서)기능을 사용 하겠습니까?", DOCUMENT_CHANGE);
					} else{
						document_group_click = 0;
					}
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_DOCUMENT_OFF:
				if (document_group_click == 0) {
				document_choice = 1;
				alertDialog("메세지(투약요청서,귀가의뢰서)기능 사용을 중지하겠습니까?", DOCUMENT_CHANGE);
				}else{
					document_group_click = 0;
				}
				break;
			}			
			break;
//CCTV.
		case R.id.MAIN_SLIDE_OPTION_RADIOGROUP_CCTV:
			switch (checkedId) {
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_CCTV_ON:
					if (cctv_group_click == 0) {
					cctv_choice = 0;
					alertDialogCCTV("CCTV 기능을 사용 하겠습니까?", CCTV_CHANGE);
					} else{
						cctv_group_click = 0;
					}
				break;
			case R.id.MAIN_SLIDE_OPTION_RADIOBUTTON_CCTV_OFF:
				if (cctv_group_click == 0) {
				cctv_choice = 1;
				alertDialogCCTV("CCTV 기능 사용을 중지하겠습니까?", CCTV_CHANGE);
				}else{
					cctv_group_click = 0;
				}
				break;
			}			
			break;
		}
	}
	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
		switch (_message.what) {
		case SUCCESS_DOCUMENT:	
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			document_group_click = 0;
			if (document_choice == 0) {
				editor.putString("PUSH_DOCUMENT", "0");
				editor.commit();		
			} else if (document_choice == 1) {
				editor.putString("PUSH_DOCUMENT", "1");
				editor.commit();		
			}
			Toast.makeText(this, (String)_message.obj, Toast.LENGTH_LONG).show();
			break;

		case FAIL_DOCUMENT:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
				Toast.makeText(this, (String)_message.obj, Toast.LENGTH_SHORT).show();
			break;
		case SUCCESS_CCTV:	
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			cctv_group_click = 0;
			if (cctv_choice == 0) {
				editor.putString("PUSH_CCTV", "0");
				editor.commit();	
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_TIMER).setVisibility(View.VISIBLE);
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_START).setVisibility(View.VISIBLE);
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_STOP).setVisibility(View.VISIBLE);
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_APPLY).setVisibility(View.VISIBLE);
				
			} else if (cctv_choice == 1) {
				editor.putString("PUSH_CCTV", "1");
				editor.commit();	
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_TIMER).setVisibility(View.GONE);
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_START).setVisibility(View.GONE);
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_STOP).setVisibility(View.GONE);
				findViewById(R.id.MAIN_SLIDE_OPTION_LINEARLAYOUT_CCTV_APPLY).setVisibility(View.GONE);
			}
			Toast.makeText(this, (String)_message.obj, Toast.LENGTH_LONG).show();
			break;
			
		case FAIL_CCTV:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			Toast.makeText(this, (String)_message.obj, Toast.LENGTH_SHORT).show();
			break;
		case SUCCESS_CCTV_TIME_LOAD:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			editor_school =  psf.getSchool_edit();
			editor_school.putString(SF_School_Key.CCTVTIMESTART, school_detail.Sch_Cctv_Time_Start);
			editor_school.putString(SF_School_Key.CCTVTIMESTOP, school_detail.Sch_Cctv_Time_Stop);
			editor_school.putString(SF_School_Key.CCTV, school_detail.Sch_Cctv);
			editor_school.commit();		
			psf.setSchoolsp();
			school = psf.getSchoolsp();
			setCctvNow();
			break;
		case SUCCESS_CCTV_TIME:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			Toast.makeText(this, (String)_message.obj, Toast.LENGTH_SHORT).show();
			editor_school =  psf.getSchool_edit();
/*			editor_school.putString(SF_School_Key.CCTVTIMESTART, school_detail.Sch_Cctv_Time_Start);
			editor_school.putString(SF_School_Key.CCTVTIMESTOP, school_detail.Sch_Cctv_Time_Stop);
			editor_school.putString(SF_School_Key.CCTV, school_detail.Sch_Cctv);
			editor_school.commit();		
			psf.setSchoolsp();
			school = psf.getSchoolsp();
			setCctvNow();   */
			break;
			
			
			
		case CATCH_EXCEPTION:	
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"Main_Slide_Option", member.Email);			
			
			break;	
		}
	}
	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho= choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case DOCUMENT_CHANGE:
					documentThread();
					break;
				case CCTV_CHANGE:
					cctvThread();
					break;
				case CCTV_TIME_CHANGE:
					Sch_Cctv_Time_Start = startTimeCctv;
					Sch_Cctv_Time_Stop = stopTimeCctv;
					cctvTimeThread();
					break;
				case CCTV_TIME_LOAD:
					cctvTimeLoadThread();
					break;
				}
			}
		});
		firstThread.start();
		startDIal();
	}
	protected void cctvTimeLoadThread() {
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_SLIDE_OPTION_CCTV_TIME_LOAD 
							+ "&PK_Sch_Code=" + member.School
							+ "&PK_Mem_Email=" + member.Email ));
			if (jsonObject.getBoolean("success")) {
				school_detail = JsonParser.school_data(jsonObject.getString("payload"));
				onNetworkCallback(SUCCESS_CCTV_TIME_LOAD, "");				
				endDIal();
			} else {
				onNetworkCallback(FAIL_CCTV_TIME_LOAD, jsonObject.getString("message"));
				endDIal();
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+" where cctvTimeLoadThread()");
			endDIal();
		}
	}

	protected void cctvTimeThread() {
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_SLIDE_OPTION_CCTV_TIME 
							+ "&PK_Sch_Code=" + member.School
							+ "&Sch_Cctv_Time_Start=" + Sch_Cctv_Time_Start
							+ "&Sch_Cctv_Time_Stop=" + Sch_Cctv_Time_Stop
							+ "&PK_Mem_Email=" + member.Email ));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_CCTV_TIME, jsonObject.getString("payload"));				
				endDIal();
			} else {
				onNetworkCallback(FAIL_CCTV_TIME, jsonObject.getString("message"));
				endDIal();
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION,e.toString()+" where cctvTimeThread()");
			endDIal();
		}
	}

	protected void cctvThread() {
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_SLIDE_OPTION_CCTV + "&PK_Sch_Code=" + member.School
							+ "&Sch_Cctv=" + cctv_choice+ "&PK_Mem_Email=" + member.Email));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_CCTV, jsonObject.getString("payload"));				
			} else {
				onNetworkCallback(FAIL_CCTV, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION,e.toString()+" where cctvThread()");
		}
	}

	protected void documentThread() {
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_SLIDE_OPTION_DOCUMENT + "&PK_Sch_Code=" + member.School
							+ "&Sch_Document=" + document_choice+ "&PK_Mem_Email=" + member.Email));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_DOCUMENT, jsonObject.getString("payload"));				
			} else {
				onNetworkCallback(FAIL_DOCUMENT, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION,e.toString()+" where documentThread()");
		}
	}	
	
	// 네.아니오.선택다이얼로그
	void alertDialog(String string_title, int int_code) {

		final String title = string_title;
		final int code = int_code;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				Main_Slide_Option.this);
		AlertDialog alert;
		builder.setMessage(title)
				.setCancelable(false)
				.setPositiveButton("네", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						document_group_click = 1;
						startNetwork(code);
					}
				})
				.setNegativeButton("아니오",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								document_group_click = 1;
								if (document_choice == 0) {
									//radioButton_On[6].setChecked(false);
									radioButton_Off[6].setChecked(true);				
								} else if (document_choice == 1) {
									radioButton_On[6].setChecked(true);
									//radioButton_Off[6].setChecked(false);				
								}
								dialog.cancel();
							}
						});
		alert = builder.create();
		alert.show();
	}
	//cctv용
	void alertDialogCCTV(String string_title, int int_code) {
		
		final String title = string_title;
		final int code = int_code;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				Main_Slide_Option.this);
		AlertDialog alert;
		builder.setMessage(title)
		.setCancelable(false)
		.setPositiveButton("네", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				cctv_group_click = 1;
				startNetwork(code);
			}
		})
		.setNegativeButton("아니오",
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,
					int which) {
				cctv_group_click = 1;
				if (cctv_choice == 0) {
					//radioButton_On[6].setChecked(false);
					radioButton_Off[7].setChecked(true);				
				} else if (cctv_choice == 1) {
					radioButton_On[7].setChecked(true);
					//radioButton_Off[6].setChecked(false);				
				}
				dialog.cancel();
			}
		});
		alert = builder.create();
		alert.show();
	}
	//CCTV시간선택용.
	void alertDialogCCTVtime(String string_title, int int_code) {
		
		final String title = string_title;
		final int code = int_code;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				Main_Slide_Option.this);
		AlertDialog alert;
		builder.setMessage(title)
		.setCancelable(false)
		.setPositiveButton("네", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				startNetwork(code);
			}
		})
		.setNegativeButton("아니오",
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,
					int which) {
				dialog.cancel();
			}
		});
		alert = builder.create();
		alert.show();
	}

	// 네.아니오.선택다이얼로그	//방해금지시간설정
	void alertDialog(String string_title, String string_key) {
		final String title = string_title;
		final String key = string_key;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				Main_Slide_Option.this);
		AlertDialog alert;
		builder.setMessage(title)
				.setCancelable(false)
				.setPositiveButton("네", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (key.equals("start")) {		//시작시간 초기화인경우
							editor.putInt("START_TIMEPICKER_HOUR", 0);
							editor.putInt("START_TIMEPICKER_MINUTE", 0);
							editor.putInt("START_TIMEPICKER_TIME", 0);
							editor.putInt("STOP_TIMEPICKER_HOUR", stopHour);
							editor.putInt("STOP_TIMEPICKER_MINUTE", stopMinute);
							editor.putInt("STOP_TIMEPICKER_TIME", stopTime);
							editor.putInt("START_TIMEPICKER_CHECK", 0); 
							editor.commit();
							
							startHour = 0;
							startMinute = 0;
							startTime = 0;
							editText[0].setText("");
							editText[0].setHint("다시 설정하여 주세요.");
							editText[0].setHintTextColor(Color.RED);
							editText[1].setTextColor(Color.BLACK);
							
						}else if (key.equals("stop")) {	//종료시간 초기화인 경우
							editor.putInt("START_TIMEPICKER_HOUR", startHour);
							editor.putInt("START_TIMEPICKER_MINUTE", startMinute);
							editor.putInt("START_TIMEPICKER_TIME", startTime);
							editor.putInt("STOP_TIMEPICKER_HOUR", 0);
							editor.putInt("STOP_TIMEPICKER_MINUTE", 0);
							editor.putInt("STOP_TIMEPICKER_TIME", 0);
							editor.putInt("STOP_TIMEPICKER_CHECK", 0);
							editor.commit();
							stopHour = 0;
							stopMinute = 0;
							stopTime = 0;
							editText[1].setText("");
							editText[1].setHint("다시 설정하여 주세요.");
							editText[1].setHintTextColor(Color.RED);
							editText[0].setTextColor(Color.BLACK);
						}
					}
				})
				.setNegativeButton("아니오",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
		alert = builder.create();
		alert.show();
	}
	// 네.아니오.선택다이얼로그 cctv
	void alertDialogCCTV(String string_title, String string_key) {
		final String title = string_title;
		final String key = string_key;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				Main_Slide_Option.this);
		AlertDialog alert;
		builder.setMessage(title)
		.setCancelable(false)
		.setPositiveButton("네", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (key.equals("start")) {

						editText[2].setText("0 시 0 분");
						editText[2].setTextColor(Color.BLACK);
						editText[3].setTextColor(Color.BLACK);
						Sch_Cctv_Time_Start = 0;
						startHourCctv = 0;
						startMinuteCctv = 0;
						startTimeCctv = 0;
						cctv_time_red = 0;
				}else if (key.equals("stop")) {

						editText[3].setText("0 시 0 분");
						editText[2].setTextColor(Color.BLACK);
						editText[3].setTextColor(Color.BLACK);
						Sch_Cctv_Time_Stop = 0;			
						stopHourCctv = 0;
						stopMinuteCctv = 0;
						stopTimeCctv = 0;
						cctv_time_red = 0;

				}
			}
		})
		.setNegativeButton("아니오",
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,
					int which) {
				dialog.cancel();
			}
		});
		alert = builder.create();
		alert.show();
	}
}
